$(function () {
    let markid = getUrlString('markid');
    let page = -1,time = '3',mainseries = '',maker = '',totalSize,key;

    historySearch();

    // 历史搜索
    function historySearch (){
        let data = {
            'markid':markid,
            'type':'toylibrary'
        };
        getData('post','/niu/apiv3/search_resultlist.php',data,function (data) {
            console.log(data);
            let append = '';
            for (let i = 0; i < data.data.length && i < 20;i++){
                append += '<span>'+data.data[i].searchkey+'</span>'
            }

            $('#searchSpan').append(append);

            $('#searchSpan span').click(function () {
                key = $(this).html();
                $('#searchVal').val(key);
                $('.notSearch').hide();
                $('.notSearch,.XDCodeContent').hide();
                $('.indexContent').show();
                $('#usedList').empty();//清空容器;
                totalSize = 0;
                page = -1;
                mRefresh.triggerDownLoading();
            })
        })
    }


    let mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#usedList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                loadpagedata(page,key)
            }
        }
    });


    //搜索
    $('#searchBtn').click(function () {
        key = $('#searchVal').val();
        if(key === '' || key === undefined){
            $.toast('搜索内容不能为空');
            $('.indexContent,.nullContent,.XDCodeContent').hide();
            $('.notSearch').show();
            return
        }
        $('.notSearch,.XDCodeContent').hide();
        $('.indexContent').show();
        $('#usedList').empty();//清空容器;
        totalSize = 0;
        page = -1;
        mRefresh.triggerDownLoading();
        // loadpagedata(page,key)
    });
    $(".search").keypress(function(e){
        if(e.keyCode === 13) {
            key = $('#searchVal').val();
            if(key === '' || key === undefined){
                $.toast('搜索内容不能为空');
                $('.indexContent,.nullContent,.XDCodeContent').hide();
                $('.notSearch').show();
                return
            }
            $('.notSearch,.XDCodeContent').hide();
            $('.indexContent').show();
            $('#usedList').empty();//清空容器;
            totalSize = 0;
            page = 0;
            mRefresh.triggerDownLoading();
            // loadpagedata(page,key)
        }
    });

    //分类搜索
    function loadpagedata (page,key){
        if(key === '' || key === undefined){
            return
        }
        var data = {
            'markid':markid,
            'page':page,
            'keywords':key,
        };
        getData('post','/niu/apiv4/niu_playsearch.php',data,function (data) {
            console.log(data)
            let append = '';
            $(data.info).each(function () {
                append += '<li>' +
                    ' <input type="hidden" value="'+this.id+'"> '+
                    '                    <div class="left">' +
                    '                        <img src="'+this.pics+'" alt="">' +
                    '                    </div>' +
                    '                    <div class="right">' +
                    '                        <div class="name">'+this.name+'</div>' +
                    '                        <div class="lable">' +
                    '                            <span>'+this.typeName+'</span>' +
                    '                            <span>'+this.maker+'</span>' +
                    '                            <span>'+this.mainseries+'</span>' +
                    '                        </div>' +
                    '                    </div>' +
                    '                </li>'
            });
            $('#usedList').append(append);

            totalSize = totalSize + 10;
            if (totalSize > data.datacount){
                mRefresh.endUpLoading(true);
            } else{
                mRefresh.resetUpLoading(true);
                mRefresh.endUpLoading(false);
            }
        })
    }

    $("#usedList li").live("click", function(){
        let id = $(this).find('input').val();
        window.location.href = 'dancool://web='+urlView+'productData/dataDetail.html?id='+id;
    });

});